package edu.scau.web.finance.skuRecord;

import edu.scau.core.constants.AjaxResult;
import edu.scau.finance.order.service.OrderReportService;
import edu.scau.finance.sku.service.SkuReportService;
import edu.scau.finance.skuRecord.service.SkuRecordReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/report")
@Tag(name = "EChat报表数据")
public class EchartReportController {
    @Autowired
    private SkuRecordReportService skuRecordReportService;
    @Autowired
    private SkuReportService skuReportService;
    @Autowired
    private OrderReportService orderReportService;


    @Operation(
            summary = "统计不同日期出入库情况",
            description = "以时间为x轴,type分类的双柱状图,访问权限:admin、IM",
            parameters = {
                    @Parameter(name = "startTime",description = "起始范围时间: xxxx-xx-xx",required = false),
                    @Parameter(name = "endTime",description = "终止范围时间: xxxx-xx-xx",required = false),
            }
    )
    @GetMapping("/skuRecord/getSkuRecordOfTypeDaily")
    @PreAuthorize("hasAnyAuthority('admin','IM')")
    public AjaxResult getSkuRecordOfTypeDaily( @RequestParam(value = "startTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)  Date startTime,@RequestParam(value = "endTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endTime) {
        return AjaxResult.success(skuRecordReportService.getSkuRecordOfTypeDaily(startTime,endTime));
    }

    @Operation(
            summary = "统计不同库存出入库情况",
            description = "以库存为x轴,type分类,访问权限:admin、IM",
            parameters = {
                    @Parameter(name = "startTime",description = "起始范围时间: xxxx-xx-xx",required = false),
                    @Parameter(name = "endTime",description = "终止范围时间: xxxx-xx-xx",required = false),
            }
    )
    @GetMapping("/getQuantityGroupedBySkuCodeAndType/")
    @PreAuthorize("hasAnyAuthority('admin','IM')")
    public AjaxResult getQuantityGroupedBySkuCodeAndType( @RequestParam(value = "startTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)  Date startTime,@RequestParam(value = "endTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endTime) {
        return AjaxResult.success(skuRecordReportService.getQuantityGroupedBySkuCodeAndType(startTime,endTime));
    }

    @Operation(
            summary = "统计不同库存的库存量",
            description = "统计不同库存的库存量,访问权限:admin、IM",
            parameters = {
                    @Parameter(name = "startTime",description = "起始范围时间: xxxx-xx-xx",required = false),
                    @Parameter(name = "endTime",description = "终止范围时间: xxxx-xx-xx",required = false),
            }
    )
    @GetMapping("/getStockGroupedBySkuName")
    @PreAuthorize("hasAnyAuthority('admin','IM')")
    public AjaxResult getStockGroupedBySkuName( @RequestParam(value = "startTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)  Date startTime,@RequestParam(value = "endTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endTime) {
        return AjaxResult.success(skuReportService.getStockGroupedBySkuName(startTime,endTime));
    }

    @Operation(
            summary = "统计每日/月/年收入",
            description = "默认统计每日订单收入,可选,访问权限:admin、Accountant",
            parameters = {
                    @Parameter(name = "startTime",description = "起始范围时间: xxxx-xx-xx",required = false),
                    @Parameter(name = "endTime",description = "终止范围时间: xxxx-xx-xx",required = false),
                    @Parameter(name = "period",description = "month/year",required = false),
            }
    )
    @GetMapping("/getDailyOrderAmount")
    @PreAuthorize("hasAnyAuthority('admin','Accountant')")
    public AjaxResult getDailyOrderAmount(@RequestParam(value = "startTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)  Date startTime,@RequestParam(value = "endTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endTime,String period) {
        return AjaxResult.success(orderReportService.getDailyOrderAmount(startTime,endTime,period));
    }

    @Operation(
            summary = "统计不同状态下的订单数量",
            description = "统计不同状态下的订单数量,访问权限:admin、Accountant、cashier",
            parameters = {
                    @Parameter(name = "startTime",description = "起始范围时间: xxxx-xx-xx",required = false),
                    @Parameter(name = "endTime",description = "终止范围时间: xxxx-xx-xx",required = false),
            }
    )
    @GetMapping("/getOrderCountGroupedByStatus")
    @PreAuthorize("hasAnyAuthority('admin','Accountant','cahier')")
    public AjaxResult getOrderCountGroupedByStatus(@RequestParam(value = "startTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)  Date startTime,@RequestParam(value = "endTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endTime) {
        return AjaxResult.success(orderReportService.getOrderCountGroupedByStatus(startTime,endTime));
    }

    @Operation(
            summary = "根据不同分类统计销售额",
            description = "根据不同分类统计销售额",
            parameters = {
                    @Parameter(name = "startTime",description = "起始范围时间: xxxx-xx-xx",required = false),
                    @Parameter(name = "endTime",description = "终止范围时间: : xxxx-xx-xx",required = false),
                    @Parameter(name = "period",description = "month/year",required = false),
            }
    )
    @GetMapping("/getOrderCountGroupedByType")
    public AjaxResult calculateAmountGroupByCategory(@RequestParam(value = "startTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)  Date startTime,@RequestParam(value = "endTime",required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endTime,String period){
        return AjaxResult.success(orderReportService.calculateAmountGroupByCategory(startTime,endTime,period));
    }
}
